package testCal;

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

// 界面对象
public class CalFrame extends JFrame {

	private JTextField textInput = null;

	private String[] resetOp = { "BackSpace", "CE", "C" };
	private String[] numCalOp = { "7", "8", "9", "/", "4", "5", "6", "*", "1",
			"2", "3", "-", "0", "=", ".", "+" };

	private ButtonAction buttonAction = new ButtonAction();
	private ActionListener actionListener = null;

	public CalFrame() {
		init();
	}

	private void init() {
		this.setTitle("java计算器");
		this.setResizable(false);
		// 主面板
		JPanel mainPanel = new JPanel();
		// 输入框面板(北)
		mainPanel.setLayout(new BorderLayout(Param.FIELD_HGAPS,
				Param.FIELD_WGAPS));
		mainPanel.add(getTextField(), BorderLayout.NORTH);
		mainPanel.setPreferredSize(new Dimension(Param.PRE_WIDTH,
				Param.PRE_HEIGHT));

		JPanel panel1 = new JPanel();
		panel1.setLayout(new GridLayout(5, 1, 0, 3));

		// 中间面板
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new BorderLayout(5, 1));

		// 中北面板 增加Back.CE.C
		JButton[] resetButton = getRButton();
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new GridLayout(1, 3, 3, 4));
		for (JButton b : resetButton) {
			northPanel.add(b);
		}

		// 中中面板 增加数字按钮和操作符
		JButton[] numButton = getNButton();
		JPanel toCenterPanel = new JPanel();
		toCenterPanel.setLayout(new GridLayout(4, 4, 3, 4));
		for (JButton b : numButton) {
			toCenterPanel.add(b);
		}

		centerPanel.add(northPanel, BorderLayout.NORTH);
		centerPanel.add(toCenterPanel, BorderLayout.CENTER);
		mainPanel.add(panel1, BorderLayout.WEST);
		mainPanel.add(centerPanel, BorderLayout.CENTER);
		this.add(mainPanel);

	}


	 /** 监听器
	 * @Param buttonText 各个按钮返回的事件
	 * @Param result 各个业务实现的最终结果
	 * @return 
	 */
	public ActionListener getActionListener() {
		if (actionListener == null) {
			actionListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					String buttonText = e.getActionCommand();
					String result = null;

					try {
						result = buttonAction.buttonMethod(buttonText,
								textInput.getText());
					} catch (Exception e1) {
						System.out.println(e1.getMessage());
					}

					if (result != null) {
						textInput.setText(result);
					}
				}
			};
		}
		return actionListener;
	}

	/**初始化输入框*/
	private JTextField getTextField() {
		if (textInput == null) {
			textInput = new JTextField("0");
			textInput.setEditable(false);
			// 输入从右边开始
			textInput.setHorizontalAlignment(JTextField.RIGHT);
			textInput.setFont(new Font("宋体", Font.BOLD, 20));
		}
		return textInput;
	}

//获取结果操作键
	private JButton[] getRButton() {
		JButton[] result = new JButton[resetOp.length];
		for (int i = 0; i < this.resetOp.length; i++) {
			JButton b = new JButton(resetOp[i]);
			b.setFont(new Font("宋体", Font.BOLD, 16));
			b.addActionListener(getActionListener());
			result[i] = b;
		}
		return result;
	}

//获取计算器的数字操作键和数字键
	private JButton[] getNButton() {
		String[] calButton = { "/", "*", "-", "+", "=" };
		JButton[] result = new JButton[numCalOp.length];
		for (int i = 0; i < numCalOp.length; i++) {
			JButton b = new JButton(numCalOp[i]);
			b.setFont(new Font("宋体", Font.BOLD, 16));
			b.addActionListener(getActionListener());
			//先乘除后加减再等于
			Arrays.sort(calButton);
			result[i] = b;
		}
		return result;
	}

}